# a = 'a'
#
#
# def foo():
#     global a
#     print(a)
#     a = 'b'
#
#
# foo()

# import time
#
#
# def foo(max_count):
#     count = 0
#     while count < max_count:
#         print(count)
#         count += 1
#
#
# def clock_it(func, *args):
#     start_time = time.time()
#     func(*args)
#     end_time = time.time()
#     print(f"execute {format(end_time - start_time)}")
#
#
# clock_it(foo, 1000)


# import time
#
#
# def clock_it_deco(func):
#     def wrapper(*args, **kwargs):
#         start_time = time.time()
#         result = func(*args, **kwargs)
#         end_time = time.time()
#         print(f"execute {format(end_time - start_time,'.2f')} s")
#         return result
#     return wrapper
#
#
# @clock_it_deco
# def foo(max_count):
#     count = 0
#     while count < max_count:
#         print(count)
#         count += 1
#     return count
#
#
# foo(10000)


# def cache_deco(func):
#     cache1 = []
#
#     def cache(*args, **kwargs):
#         nonlocal cache1
#         cache1 = func(*args, **kwargs)
#         cache1.append(cache1)
#         return cache1
#     return cache


# def cache_deco(func, *args):
#     count = 1
#     cache = []
#     result = func(*args)
#     cache.append(result)
#     print(cache)
#
#
# # @cache_deco
# def fib(n):
#     print("called")
#     if n < 2:
#         return n
#     return fib(n-1) + fib(n-2)
#
#
# cache_deco(fib, 10)


# def fibonacci(n, cache=None):
#     if cache is None:
#         cache = {}
#
#     if n in cache:
#         return cache[n]
#
#     if n <= 1:
#         return n
#     cache[n] = fibonacci(n - 1, cache) + fibonacci(n - 2, cache)
#     return cache[n]
#
#
# for i in range(0, 10):
#     print(fibonacci(i))


def cache_deco(func):
    # 闭包，在这个里面永远能访问到cache
    cache = {}

    def wrap(*args):
        if args not in cache:
            cache[args] = func(*args)
        return cache[args]
    return wrap


@cache_deco
def fib(n):
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)


for i in range(10):
    print(fib(i), end=" ")

